package com.allin.social;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.allin.social.test.TestPullAndLoadActivity;
import com.allin.social.test.TestPullRefreshActivity;
import com.allin.social.test.TestRecycleViewAdapterActivity;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengRegistrar;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.ShareContent;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMVideo;
import com.umeng.socialize.media.UMusic;
import com.umeng.update.UmengUpdateAgent;

import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private Button btn_share;
    private Button btn_share_cus;
    private Button btn_remove_sina;
    private Button btn_remove_weixin;
    private Button btn_pull_load;
    private Button btn_pull_and_load;
    private Button btn_BGA_adapter;

    private UMShareAPI mShareAPI;
    private PushAgent mPushAgent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        UmengUpdateAgent.setDeltaUpdate(false);
        //友盟更新
        UmengUpdateAgent.update(this);

        //友盟推送
        mPushAgent = PushAgent.getInstance(this);
        mPushAgent.enable();
        mPushAgent.setPushCheck(false);    //默认不检查集成配置文件
        mPushAgent.setDebugMode(true);

        String device_token = UmengRegistrar.getRegistrationId(this);
        Log.i("device_token", "device_token==" + device_token);




        btn_share = (Button) findViewById(R.id.button);
        btn_share_cus = (Button) findViewById(R.id.button2);
        btn_remove_sina = (Button) findViewById(R.id.button3);
        btn_remove_weixin = (Button) findViewById(R.id.button4);
        btn_pull_load = (Button) findViewById(R.id.button5);
        btn_pull_and_load = (Button) findViewById(R.id.button6);
        btn_BGA_adapter = (Button) findViewById(R.id.button7);
        btn_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                UMImage image = new UMImage(MainActivity.this, "http://www.umeng.com/images/pic/social/integrated_3.png");
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                UMImage image = new UMImage(MainActivity.this, bitmap);
                UMusic music = new UMusic("http://music.huoxing.com/upload/20130330/1364651263157_1085.mp3");
                music.setTitle("测试新版");
                music.setThumb(new UMImage(MainActivity.this, "http://www.umeng.com/images/pic/social/chart_1.png"));
                UMVideo video = new UMVideo("http://video.sina.com.cn/p/sports/cba/v/2013-10-22/144463050817.html");

                new ShareAction(MainActivity.this)
                        .setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SMS)
                        .setContentList(new ShareContent(), new ShareContent())
                        .withMedia(image)
                        .setListenerList(umShareListener, umShareListener, umShareListener)
                        .open();
            }
        });

        btn_share_cus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent= new Intent(MainActivity.this,CustomShareActivity.class);
                startActivity(intent);
            }
        });


        //取消授权
        mShareAPI = UMShareAPI.get( this );

        btn_remove_sina.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mShareAPI.deleteOauth(MainActivity.this, SHARE_MEDIA.SINA, umdelAuthListener);
            }
        });

        btn_remove_weixin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mShareAPI.deleteOauth(MainActivity.this, SHARE_MEDIA.WEIXIN, umdelAuthListener);
            }
        });

        btn_pull_load.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, TestPullRefreshActivity.class));
            }
        });

        btn_pull_and_load.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, TestPullAndLoadActivity.class));
            }
        });

        btn_BGA_adapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                startActivity(new Intent(MainActivity.this, TestBGAAdapterActivity.class));
                startActivity(new Intent(MainActivity.this, TestRecycleViewAdapterActivity.class));
            }
        });
    }


    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(MainActivity.this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(MainActivity.this, platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(MainActivity.this, platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };


    //授权回调
    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            Toast.makeText( getApplicationContext(), "Authorize succeed", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText( getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText( getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show();
        }
    };

    //删除授权回调
    private UMAuthListener umdelAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            Toast.makeText(getApplicationContext(), "delete Authorize succeed", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText( getApplicationContext(), "delete Authorize fail", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText( getApplicationContext(), "delete Authorize cancel", Toast.LENGTH_SHORT).show();
        }
    };


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /** attention to this below ,must add this**/
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

}
